package com.example.tests;

import static org.testng.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.Assert.assertThat;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.example.fw.AccountHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignupTest extends TestBase{
	
	public User user = new User()
	.setLogin("test1")
	.setPassword("password")
	.setEmail("test1@localhost.localdomain");
	
	private JamesHelper james;

	private AccountHelper account;
	
	/*user.setLogin("testuser1");
	.setPassword("123456").setEmail("testuser1@localhost.localdomain");*/
	
	
	@BeforeClass
	public void createMailUser() {
		james = app.getJamesHelper();
		account = app.getAccountHelper();
		if (! james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}	
	}
	
	@AfterClass
	public void deleteMailUser() {
		if (james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
		}	
	}
	
	@Test
	public void newUserShouldSighup() {
		if (account.userExists(user)) {
		account.deleteUser(user);
		}
		account.signup(user);
		account.login(user);
		assertThat(account.loggedUser(), equalTo(user.login));
	}
	
	//@Test
	public void existingUserShouldNotSighup() {
		try {
			account.signup(user);
		}  catch (Exception e) {
			assertThat(e.getMessage(), containsString("That username is already being used"));
			return;
		}
		fail("Exception expected");
	}
	
	
}
